package com.wdc.deliveryrobot.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.sirui.selfstudysdk.main.SelfChassisState;
import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.dialog.HintDialog;
import com.wdc.deliveryrobot.model.Point;

import java.util.List;

public class PointListAdapter extends RecyclerView.Adapter<PointListAdapter.ViewHolder>{

    private List<Point> locations;

    private Context mContext;

    private SelfChassisState selfChassisState;
    private int selectedPosition = -1; // 跟踪当前选中的按钮

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Point location,Integer type);
    }

    public PointListAdapter(Context context, List<Point> locations, SelfChassisState selfChassisState, OnItemClickListener listener) {
        this.mContext = context;
        this.locations = locations;
        this.selfChassisState = selfChassisState;
        this.listener = listener;
    }

    @NonNull
    @Override
    public PointListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_point, parent, false);
        return new PointListAdapter.ViewHolder(view);


    }

    @Override
    public void onBindViewHolder(@NonNull PointListAdapter.ViewHolder holder, int position) {
        Point point = locations.get(position);
        String pointList = "X:" + point.getX() + " Y:" + point.getY() + " YAW:" + point.getYaw();

        // 设置 TextView 的内容
        holder.pointNameTextView.setText(point.getName());
        holder.pointLocationTextView.setText(pointList);
        if (point.getType().equals(1)){
            holder.pointTypeTextView.setText(mContext.getString(R.string.common));
        }else {
            holder.pointTypeTextView.setText("未知");
        }


        // 处理按钮点击事件
        holder.textViewNavigation.setOnClickListener(v -> {

            // 调用外部的点击事件
//            if (listener != null) {
//                listener.onItemClick(pointLocation); // 传递点击的 location
//            }
        });

        holder.textViewDelete.setOnClickListener(view -> {
            HintDialog hintDialog = new HintDialog(view.getContext());
            hintDialog.setMessage(mContext.getString(R.string.delete_point,point.getName()));
            hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null)
                        listener.onItemClick(point,1);
                }
            });
            hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    hintDialog.dismiss();
                }
            });
            hintDialog.show();
        });

        holder.textViewNavigation.setOnClickListener(view -> {
            HintDialog hintDialog = new HintDialog(view.getContext());
            hintDialog.setMessage(mContext.getString(R.string.navigationPoint,point.getName()));
            hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null){
                        listener.onItemClick(point,2);
                    }

                }
            });
            hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    hintDialog.dismiss();
                }
            });
            hintDialog.show();
        });
    }

    @Override
    public int getItemCount() {
        return locations.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        Button textViewDelete;

        Button textViewNavigation;

        TextView pointNameTextView;
        TextView pointLocationTextView;
        TextView pointTypeTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            pointNameTextView = itemView.findViewById(R.id.pointName);
            pointLocationTextView = itemView.findViewById(R.id.point_location);
            pointTypeTextView = itemView.findViewById(R.id.pointType);
            textViewDelete = itemView.findViewById(R.id.button_delete);
            textViewNavigation = itemView.findViewById(R.id.button_navigation);
        }
    }

}
